<?php
class Goods_model extends MY_Model
{
    private $table = 'blues_goods';
    public function __construct()
    {
        parent::__construct();
    }
    public function get_goods_list($fields = "*",$per_page,$limit)
    {
        $where = " onlinestatus=1 AND isDel=0";
        if($per_page){
                $per_page = ($per_page-1)*$limit;
        }   
        $limit = " limit {$per_page},{$limit}";
       
		$sql = "SELECT {$fields} FROM {$this->table} WHERE {$where} ORDER BY addtime DESC {$limit}";
		$query = $this->db->query($sql);
        return $query->result_array();        
    }
    //根据itemid获取商品默认封面
    public function get_default_cover($goodsid){
        $sql = "SELECT imgurl FROM blues_goods_attach WHERE goodsid = {$goodsid} AND is_default=1";
        $query = $this->db->query($sql);
        $result = $query->row_array();
        return $result['imgurl'];
    }
  
    
    //根据产品信息获取产品详情
    public function get_goods_by_id($id, $fields = " * ")
    {
        $sql = "SELECT {$fields} FROM {$this->table} WHERE id = {$id}";
        $query = $this->db->query($sql);
        return $query->row_array();
    }   
     //获取图片列表
    public function get_img_list_byid($goodsid, $fields = " * "){
        $sql = "SELECT {$fields} FROM blues_goods_attach WHERE goodsid = {$goodsid} order by is_default desc";
        $query = $this->db->query($sql);
        return $query->result_array();
    }


    public function editAttentionRate($itemid){
        $sql = "UPDATE yy_item SET attentionRate=attentionRate+1 WHERE itemid= {$itemid}";
        $res = $this->db->query($sql);
        return $res;
    }

    public function submit($data){
        $res = $this->db->insert("blues_goods_orders",$data);
        return $res;
    }
    public function minsPoints($AccountID,$data = array()){
        $res = false;
        if($AccountID){
            $this->db->where("AccountID", $AccountID);
            $res = $this->db->update("blues_user", $data);
        }
        
        return $res;
    }


    public function getOrderList($AccountID,$per_page, $limit, $fields = "*"){
        if($per_page){
                $per_page = ($per_page-1)*$limit;
        }   
        $limit = " limit {$per_page},{$limit}";
        $sql = "SELECT {$fields} FROM blues_goods_orders WHERE AccountID = '{$AccountID}'and isDel =0 order by id desc {$limit}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function getOrderDetail($id,$AccountID)
    {
         $sql = "SELECT orders.*,goods.price FROM blues_goods_orders orders ,blues_goods goods WHERE orders.goodsID=goods.id and orders.AccountID = '{$AccountID}'and orders.isDel =0";
        $query = $this->db->query($sql);
        return $query->row_array();
    }


    public function delOrder($orderId, $AccountID){
        $this->db->where(array('AccountID'=>$AccountID,'id'=>$orderId));
        $result = $this->db->update("blues_goods_orders",array("isDel"=>1));
        return $result;
    }
	
	public function orderInfoByOrdernum($ordernum)
	{
		$this->db->where("ordernum",$ordernum);
		$query = $this->db->get("blues_goods_orders");
		return  $query->row_array();
		
	}

    public function editGoodsStock($id){
        $sql = "UPDATE blues_goods SET stock=stock-1 WHERE id= {$id}";
        $res = $this->db->query($sql);
        return $res;
    }

}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */